# Contributor: Oleg A. Khlybov <fougas@mail.ru>

_realname=mumps

pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.3.5
pkgrel=2
arch=('any')
pkgdesc="Sparse direct SLAE solver (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-metis"
         "${MINGW_PACKAGE_PREFIX}-parmetis"
         "${MINGW_PACKAGE_PREFIX}-scotch"
         "${MINGW_PACKAGE_PREFIX}-scalapack"
         "${MINGW_PACKAGE_PREFIX}-msmpi")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-gcc-fortran")
optdepends=("${MINGW_PACKAGE_PREFIX}-tcl: build & run test suite")
options=('strip' 'staticlibs')
license=('CeCILL-C')
url="http://mumps.enseeiht.fr/"
source=("http://mumps.enseeiht.fr/MUMPS_${pkgver}.tar.gz"
        "0001-makefile.inc.patch"
        "all.tcl"
        "tclbuildtest.tcl"
        "mumps.test")
sha256sums=('e5d665fdb7043043f0799ae3dbe3b37e5b200d1ab7a6f7b2a4e463fd89507fa4'
            '394a9e6de6147c33881b7935a2a70f4803eea537faf7b8c74b7cb8236c7f1f87'
            'ec5072630e1c0309fe383669e9187790cd135a393c67bc4bc35cf60b0ba396ff'
            '15c7af25b91406d5fe5f26cfe00963b6cfde1c3dd466eb25f1b6fae299934966'
            '0b58c5bb34403a69bc4c4ab1ab551876695bff1f2be66ecaa9b5528a74e8f8f6')

prepare() {
  cd "${srcdir}/${_realname}_${pkgver}"
  for p in ${srcdir}/*.patch; do
    patch -p1 -i $p
  done
}

seq_pc="metis scotch openblas"
shm_pc=$seq_pc
mpi_pc="parmetis ptscotch scalapack openblas msmpi"

build() {
  cd "${srcdir}/${_realname}_${pkgver}"
  for mod in SEQ SHM PAR; do
    cp Makefile.inc.${mod} Makefile.inc
    make -j1 clean s d c z
    if [[ $mod == SEQ ]]; then
      cp libseq/libmpiseq_seq.a lib/libmumps_mpi_seq.a
      cp PORD/lib/libpord.a lib/libmumps_pord.a
    fi
  done
  cd lib
  rm -rf libmumps-*.a
  strip -S *.a # Thin archives require the constituent archives to be stripped beforehand
  for x in c z s d; do
    ar crsT libmumps-${x}so.a lib${x}mumps_seq.a libmumps_common_seq.a libmumps_pord.a libmumps_mpi_seq.a
    ar crsT libmumps-${x}to.a lib${x}mumps_shm.a libmumps_common_shm.a libmumps_pord.a libmumps_mpi_seq.a
    ar crsT libmumps-${x}mo.a lib${x}mumps_mpi.a libmumps_common_mpi.a libmumps_pord.a
    # TODO export only the required driver routine to reduce the exported symbol table size
    gfortran -shared -Wl,--enable-auto-import -Wl,--export-all-symbols -o libmumps-${x}so.dll -Wl,--out-implib,libmumps-${x}so.dll.a -Wl,--whole-archive libmumps-${x}so.a -Wl,--no-whole-archive -lesmumps $(pkg-config ${seq_pc} --libs)
    gfortran -shared -fopenmp -Wl,--enable-auto-import -Wl,--export-all-symbols -o libmumps-${x}to.dll -Wl,--out-implib,libmumps-${x}to.dll.a -Wl,--whole-archive libmumps-${x}to.a -Wl,--no-whole-archive -lesmumps $(pkg-config ${shm_pc} --libs)
    mpifort -shared -fopenmp -Wl,--enable-auto-import -Wl,--export-all-symbols -o libmumps-${x}mo.dll -Wl,--out-implib,libmumps-${x}mo.dll.a -Wl,--whole-archive libmumps-${x}mo.a -Wl,--no-whole-archive -lptesmumps $(pkg-config ${mpi_pc} --libs)
  done
}

package() {
  cd "$srcdir/${_realname}_${pkgver}"
  mkdir -p ${pkgdir}${MINGW_PREFIX}/{bin,lib,include/mumps/mpi_seq} ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig ${pkgdir}${MINGW_PREFIX}/share/test/mumps
  (
    cd libseq
    install -m644 mpi*.h "${pkgdir}${MINGW_PREFIX}/include/mumps/mpi_seq"
  )
  (
    cd lib
    rm -f libpord*.a
    install -m644 *.a "${pkgdir}${MINGW_PREFIX}/lib"
    install -m644 *.dll "${pkgdir}${MINGW_PREFIX}/bin"
  )
  (
    cd include
    install -m644 *.h "${pkgdir}${MINGW_PREFIX}/include"
  )
  (
    cp ${srcdir}/{all.tcl,tclbuildtest.tcl,mumps.test} ${pkgdir}${MINGW_PREFIX}/share/test/mumps
    cd "$srcdir/${_realname}_${pkgver}/examples"
    cp c_example.c ?simpletest.F input_simpletest_* ${pkgdir}${MINGW_PREFIX}/share/test/mumps
  )
  for x in c z s d; do
    case $x in
      c) scalar='single precision complex';;
      z) scalar='double precision complex';;
      s) scalar='single precision';;
      d) scalar='double precision';;
    esac
    echo "
      prefix=${MINGW_PREFIX}
      libdir=\${prefix}/lib
      includedir=\${prefix}/include
      Name: ${_realname}
      URL: ${url}
      Version: ${pkgver}
      Description: Sequential $scalar MUMPS build
      Requires.private: ${seq_pc}
      Cflags: -I\${includedir}/mumps/mpi_seq
      Libs.private: -lmumps-${x}so -lesmumps -lgfortran -lquadmath
      Libs: -lmumps-${x}so
    " | sed '/^\s*$/d;s/^\s*//' > "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/${_realname}-${x}so.pc"
    echo "
      prefix=${MINGW_PREFIX}
      libdir=\${prefix}/lib
      includedir=\${prefix}/include
      Name: ${_realname}
      URL: ${url}
      Version: ${pkgver}
      Description: OpenMP $scalar MUMPS build
      Requires.private: ${shm_pc}
      Cflags: -I\${includedir}/mumps/mpi_seq
      Libs.private: -fopenmp -lmumps-${x}to -lesmumps -lgfortran -lquadmath
      Libs: -lmumps-${x}to
    " | sed '/^\s*$/d;s/^\s*//' > "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/${_realname}-${x}to.pc"
    echo "
      prefix=${MINGW_PREFIX}
      libdir=\${prefix}/lib
      includedir=\${prefix}/include
      Name: ${_realname}
      URL: ${url}
      Version: ${pkgver}
      Description: MPI+OpenMP $scalar MUMPS build
      Cflags:
      Requires.private: ${mpi_pc}
      Libs.private: -fopenmp -lmumps-${x}mo -lptesmumps -lgfortran -lquadmath
      Libs: -lmumps-${x}mo
    " | sed '/^\s*$/d;s/^\s*//' > "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/${_realname}-${x}mo.pc"
  done
}
